﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class EditNoteDialog : ChildWindow
    {
        private int currentEmployeeID;
        private DateTime currentDate;

        public EditNoteDialog()
        {
            InitializeComponent();
        }

        public async void init(int employeeID, DateTime selectedDate)
        {
            this.IsEnabled = false;
            // Initialize the current employee ID
            currentEmployeeID = employeeID;
            currentDate = selectedDate;
            await Globals.db.GetScheduleNoteObjects("EmployeeID=" + employeeID.ToString() + " and DATEDIFF(day,ScheduleDate,'" + 
                selectedDate.ToShortDateString()+"')=0 and Manager="+(Globals.currentLogin.col_Manager?"1":"0"));
            if (Globals.db.ScheduleNoteRecords.Count() > 0)
            {
                // Populate the note text box with the stored text
                tbNoteText.Text = Globals.db.ScheduleNoteRecords[0].col_NoteText;
            }
            this.IsEnabled = true;
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            if (Globals.db.ScheduleNoteRecords.Count() > 0)
            {
                Globals.db.ScheduleNoteRecords[0].col_NoteText = tbNoteText.Text;
                Globals.db.ScheduleNoteRecords[0].bFilterNoteText = true;
                await Globals.db.UpdateScheduleNoteObject(Globals.db.ScheduleNoteRecords[0]);
            }
            else
            {
                // Create a new note and add it to the database
                ServiceReferenceOpenRPOS.ScheduleNote sn = new ServiceReferenceOpenRPOS.ScheduleNote();
                sn.col_EmployeeID = currentEmployeeID;
                sn.col_ScheduleDate = currentDate;
                sn.col_NoteText = tbNoteText.Text.Trim();
                sn.col_Manager = Globals.currentLogin.col_Manager;
                sn.bFilterEmployeeID = true;
                sn.bFilterNoteText = true;
                sn.bFilterScheduleDate = true;
                sn.bFilterManager = true;
                await Globals.db.AddScheduleNoteObject(sn);
            }
            this.IsEnabled = true;
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbNoteText_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

